package main

import (
	"fmt"
	"net/http"
)

type Context struct {
	req      *http.Request
	w        http.ResponseWriter
	index    int64
	handlers []HandleFunc
}

func (c *Context) Next() {
	c.index++
	if len(c.handlers) > 0 {
		c.handlers[c.index](c)
	}
}
func (c *Context) Abort() {
	c.index = int64(len(c.handlers))
}

type HandleFunc func(*Context)

type Engine struct {
	handlers []HandleFunc
}

func (e *Engine) Use(f HandleFunc) {
	e.handlers = append(e.handlers, f)
}
func Auth(c *Context) {
	fmt.Println("权限中间件")
}
func Log(c *Context) {
	fmt.Println("日志中间件")
}

func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	ctx := &Context{req: req, w: w, index: -1, handlers: e.handlers}
	ctx.Next()
}
func main() {
	r := Engine{}
	r.Use(Auth)
	r.Use(Log)
	err := http.ListenAndServe(":8080", &r)
	if err != nil {
		return
	}
}
